package com.zzx.util;

import java.io.Serializable;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;

/**
 * 
 * @ClassName: BaseController
 * @Description: WEB 请求参数处理封装
 * @author zzx
 * @date 2021年5月24日 下午2:37:45
 *
 */
public class BaseController implements Serializable {
	/**
	 * serialVersionUID :
	 */
	private static final long serialVersionUID = 8445423708603913294L;
	
    private static final ThreadLocal<Model> modelContainer = new ThreadLocal<>();
    
    @Autowired
    public HttpServletRequest request;
    @Autowired
    public HttpServletResponse response;
    @Autowired
    public HttpSession session;

	@ModelAttribute
	public void init(Model model) {
		modelContainer.set(model);
	}
	
	public HttpServletRequest getRequest() {
        return request;
    }
    public HttpServletResponse getResponse() {
        return response;
    }
    
    public Model getModel() {
        return modelContainer.get();
    }
    
	public HttpSession getSession() {
		return session;
	}

	public String getPara(String name) {
		String value = request.getParameter(name);
		return value != null ? value.trim() : value;
	}

	public void setAttr(String key, Object obj) {
		getModel().addAttribute(key, obj);
	}

	public void setSessionAttr(String key, Object obj) {
		session.setAttribute(key, obj);
	}

	public Object getSessionAttr(String key) {
		return session.getAttribute(key);
	}

	public String getStringSessionAttr(String key) {
		return (String) getSessionAttr(key);
	}
}
